const helper = require('./helper.js');
const dataHelper = require('./data_helper.js');
const cacheHelper = require('./cache_helper.js');
const constants = require('../comm/constants.js');
const contentCheckHelper = require('./content_check_helper.js');
const pageHelper = require('./page_helper.js');
const timeHelper = require('./time_helper.js');
const setting = require('@/utils/setting/setting.js');
async function dataList(that, listName, route, params, options, isReverse = false) {
    // console.log('dataList begin');
    if (!helper.isDefined(that[listName]) || !that[listName]) {
        let data = {};
        data[listName] = {
            pageNo: 1,
            pageSize: 20,
            list: [],
            pageCount: 0,
            total: 0,
            oldTotal: 0,
            condition: ''
        };
        that.setData(data);
    }

    //改为后台默认控制
    //if (!helper.isDefined(params.size))
    //	params.size = 20;

    if (!helper.isDefined(params.isTotal)) {
        params.isTotal = true;
    }
    let pageNo = params.pageNo;
    let count = that[listName].pageCount;
    if (pageNo > 1 && pageNo > count) {
        uni.showToast({
            duration: 500,
            icon: 'none',
            title: '没有更多数据了'
        });
        return;
    }
    // for (let key in params) {
    //     if (!helper.isDefined(params[key])) {
    //         delete params[key];
    //     }
    // }
    let oldTotal = 0;
    if (that[listName] && that[listName].total) {
        oldTotal = that[listName].total;
    }
    params.oldTotal = oldTotal;
    // let listData = {list:[]};
    // listData[listName] = listData;
    // that.setData(listData);
    // uni.$u.http.post('/notice/list'); 
    // 云函数调用
    await uni.$u.http.post(route, params, options)
        .then(function (res) {
            // console.log('cloud begin', res);
            let dataList = res;
            let tList = that[listName].list;
            
            // 获取当前请求的页码，优先使用服务器返回的页码，否则使用请求参数中的页码
            let currentPage = dataList.pageNo || params.pageNo || 1;
            
            // console.log('currentPage:', currentPage, 'stored page:', that[listName].pageNo);
            
            if (currentPage == 1) {
                // 第一页：替换所有数据
                tList = res.list || [];
            } else if (currentPage > (that[listName].pageNo || 0)) {
                // 后续页：追加数据
                if (isReverse) {
                    tList = (res.list || []).concat(tList);
                } else {
                    tList = tList.concat(res.list || []);
                }
            } else {
                // console.log('页码异常，跳过数据合并');
                return;
            }
            
            // 统一数据结构，确保字段名一致（按照后端约定：pageNo, pageSize, pageCount, total, list）
            dataList = {
                list: tList,
                pageNo: currentPage,
                pageSize: dataList.pageSize || 20,
                pageCount: dataList.pageCount || 0,
                total: dataList.total || 0,
                oldTotal: dataList.oldTotal || 0,
                condition: dataList.condition || ''
            };
            
            let listData = {};
            listData[listName] = dataList;
            that.setData(listData);
            // console.log('cloud END', 'total items:', tList.length);
        })
        .catch((err) => {
            console.log(err);
        });
    // console.log('dataList END');
}
module.exports = {
    dataList
}
